package edu.zcmu.seckill.rocket.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import edu.zcmu.seckill.entity.Order;
import edu.zcmu.seckill.entity.Product;
import edu.zcmu.seckill.entity.SeckillOrder;
import edu.zcmu.seckill.enumeration.GlobalEnum;
import edu.zcmu.seckill.enumeration.OrderStatusEnum;
import edu.zcmu.seckill.rocket.to.OrderTo;
import edu.zcmu.seckill.service.OrderService;
import edu.zcmu.seckill.service.ProductService;
import edu.zcmu.seckill.service.SeckillOrderService;
import edu.zcmu.seckill.service.SeckillProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
@RocketMQMessageListener(topic = "Order",consumerGroup = "my-consumer-group")
public class OrderConsumer implements RocketMQListener<OrderTo> {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    SeckillOrderService seckillOrderService;

    @Autowired
    SeckillProductService seckillProductService;
    @Override
    @Transactional
    public void onMessage(OrderTo orderTo) {
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        String s = operations.get("sekillproduct:" + orderTo.getProductId());
        Product product = JSON.parseObject(s, Product.class);

        SeckillOrder order = new SeckillOrder();
        order.setOrderId(orderTo.getId());
        order.setSeckillId(Integer.valueOf(orderTo.getProductId()));
        order.setUserId(Integer.valueOf(orderTo.getUserId()));
        order.setOrderStatus(OrderStatusEnum.ORDER_WAITING.getCode());
        order.setProductName(product.getName());
        order.setTotalPrice(product.getPrice());
        order.setCreateTime(this.getNowDate());
        System.out.println(order);
        int a = seckillOrderService.createSeckillOrder(order);
        if (a!=1){
            RSemaphore semaphore = redissonClient.getSemaphore("sekillproduct:count:" + orderTo.getProductId());
            semaphore.release();
            redisTemplate.delete("seckill:order:"+orderTo.getProductId()+":"+orderTo.getUserId());
            redisTemplate.delete(GlobalEnum.USER_SECKILL_PRODUCT_IS_EXPIRATION.getCode() + orderTo.getUserId() + ":" + orderTo.getProductId()+":"+orderTo.getId());
            log.info("创建订单失败");
        }
        log.info("用户id：{}   创建订单成功",orderTo.getUserId());
    }

    public static Date getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        try {
            Date parse = formatter.parse(dateString);
            return parse;
        }catch (Exception e){
            return currentTime;
        }
    }
}